package com.example.demo.zaqizabatest;

/**
 * @ClassName King8
 * @Author lys4134
 * @Date 2022/01/06 10:57:34
 * @Description King8
 * @Version 1.0
 **/
public class King8 {
    private int max = 8;
    //存放八皇后二维数组下标
    private int[] array = new int[max];

    private int count = 0;

    public void print() {
        for (int i = 0; i < max; i++) {
            System.out.print(array[i] + " ");
        }
        System.out.println();
        count++;
    }

    /**
     *
     * @param n 第n+1给皇后，第八个皇后，n=7
     * @return
     */
    public boolean judge(int n) {
        for (int i = 0; i < n; i++) {
            if(array[i] == array[n] || Math.abs(n - i) == Math.abs(array[n] - array[i])) {
                return false;
            }
        }
        return true;
    }

    public void insert(int n) {
        if (n == max) { // 说明这是第九个皇后
            print();
            return;
        }

        for (int i = 0; i < max; i++) {
            array[n] = i;
            if(judge(n)) {
                insert(n + 1);
            }
        }
    }

    public int getCount() {
        return count;
    }

    public static void main(String[] args) {
        King8 king8 =new King8();
        king8.insert(0);
        System.out.println(king8.getCount());
    }
}
